<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$merch=model('jy_merch/model')->checkmerch();
$this->model('model')->checkvip();
if($_RQ['type']){
	$this->session('type',$_RQ['type']);
}
$type=$this->session('type');
if($_RQ['op']=='main'){
	$user=pdo_get('jy_shopbaojia_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	set_html('title','我的报价');
	$return['params']=array('type'=>$type?:'wechat','keyword'=>'');
	if($_RQ['keyword']){
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$return['list_status']=$this->list_status;
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merch['id']);
	if($_RQ['merchid']){
		$params['merchid']=intval($_RQ['merchid']);	
	}
	if($_RQ['keyword']){
		$isnum=pdo_count('jy_shopbaojia_list',array('title like'=>'%'.$_RQ['keyword'].'%','uniacid'=>$_SESSION['uniacid'],'merchid'=>$merch['id']),'id');
		if($isnum<=0){
			$params['productsn like']='%'.$_RQ['keyword'].'%';
		}else{
			$params['title like']='%'.$_RQ['keyword'].'%';
		}
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_shopbaojia_list',$params,array($pindex,$psize),$total,$fields,'','updatetime desc');
	foreach($list as &$kv){
			$kv['image']=tomedia($kv['image']);
	}
	//按点赞数排序作品
	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['image']=tomedia($item['image']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$item['likenum']=pdo_count('jy_shopbaojia_shoucang',array('listid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'isshoucang'=>1),'id');
			$quan_log=pdo_get('jy_shopbaojia_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'listid'=>$item['id']));
			if($quan_log['id']){
				if($quan_log['isadd']==1){
					$item['islook']=2;
				}else{
					$item['islook']=1;
				}
			}
			
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_shopbaojia_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3);
	}
	$item['videos']=$item['videos']?json_decode($item['videos'],true):array();
	$return['item']=$item;
	$return['groups']=pdo_getall('jy_shopbaojia_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>0),array('id','title'),'id');
	foreach($return['groups'] as &$gv){
		$gv['cids']=pdo_getall('jy_shopbaojia_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$gv['id']),array('id','title'),'id');
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['merchid']){
		//exi('请选择发布的商户！','error');
	}
	if(!$postdata['title']){
		exi('请填写商品名称！','error');
	}
	if(!$postdata['productsn']){
		//exi('请填写商品编码！','error');
	}
	if(!$postdata['image']){
		//exi('请填写商品图片！','error');
	}
	if(!$postdata['price']){
		exi('请填写报价！','error');
	}
	if(!$postdata['group_id']){
		//exi('请填写商品分类！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'desc'=>trim($postdata['desc']),
		'readnum'=>intval($postdata['readnum']),
		'cid'=>intval($postdata['cid']),
		'group_id'=>intval($postdata['group_id']),
		'price'=>trim($postdata['price']),
		'brand'=>trim($postdata['brand']),
		'unit'=>trim($postdata['unit']),
		'optiontitle'=>trim($postdata['optiontitle']),
		'productsn'=>trim($postdata['productsn']),
		'noshow'=>intval($postdata['noshow']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_shopbaojia_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
		if($this->setting['kan_upfee'] > $member['credit3']){
			exi('您的金币不足，不能参与！','error');
		}
		if($this->setting['kan_upfee']>0){
			$data['status']=1;
			model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->setting['kan_upfee'],true,'jy_shopbaojia','发布报价扣金币');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=intval($merch['id']);
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_shopbaojia_list',$data);
	}
	exi('操作成功！','','jy_shopbaojia/mylist/main/');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('报价不存在哦！','error');
	}
	pdo_delete('jy_shopbaojia_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功！','','/mylist/');

}